package com.example.demo.controller;

import com.example.demo.util.ResponseUtils;
import com.example.demo.util.dto.Data;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping(value = "/demo")
@Slf4j
public class DemoController {

    @Autowired
    private ValueOperations<String, String> strOperations;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ValueOperations<String, Integer> intOperations;
    @Autowired
    private RedisTemplate<String, Integer> intRedisTemplate;

    public static final String PREFIX = "mycounter_";


    @GetMapping("/v2")
    @ApiOperation(value = "方法v2")
    public ResponseEntity<Data<String>> demoV2() {


        // 2.保存数据
        strOperations.set("name", "imooc2");
        // 3. 获取数据
        String value = strOperations.get("name");


        log.info("记个日志：{}", value);
        return ResponseUtils.res(value);
    }

    @GetMapping("/v3")
    @ApiOperation(value = "方法v3")
    public ResponseEntity<Data<String>> demoV3() {


        // 2.保存数据
        stringRedisTemplate.opsForValue().set("name", "imooc3");
        // 3. 获取数据
        String value = stringRedisTemplate.opsForValue().get("name");


        log.info("记个日志：{}", value);
        return ResponseUtils.res(value);
    }

    /**
     * 数量控制器v1
     *
     * @return
     */
    @GetMapping("/count/v1")
    @ApiOperation(value = "数量控制器v1")
    public ResponseEntity<Data<String>> countV1() {
        String key = PREFIX + "v1";
        int amountLimit = 100;
        int incrAmount = 1;

        if (Objects.isNull(intOperations.get(key))) {
            intOperations.set(key, 95);
        }

        Integer currAmount = intOperations.get(key);

        if (currAmount + incrAmount > amountLimit) {
            log.info(" Bad Luck :{},{},currAmount + incrAmount > amountLimit={}", key, amountLimit,currAmount + incrAmount > amountLimit);

        } else {
            intOperations.set(key, currAmount + incrAmount);
            log.info(" Good Luck :{},{},currAmount + incrAmount > amountLimit={}", key, amountLimit,currAmount + incrAmount > amountLimit);
        }

        return ResponseUtils.res(currAmount.toString());
    }

    /**
     * 数量控制器v2
     *
     * @return
     */
    @GetMapping("/count/v2")
    @ApiOperation(value = "数量控制器v2")
    public ResponseEntity<Data<String>> countV2() {
        String key = PREFIX + "v2";
        int amountLimit = 100;
        Long incrAmount = 1L;
        int startValue = 95;
        if (!intRedisTemplate.hasKey(key)) {
            intRedisTemplate.opsForValue().setIfAbsent(key, startValue);
        }

        Integer currAmount = intRedisTemplate.opsForValue().get(key);
        Long increment = intRedisTemplate.opsForValue().increment(key, incrAmount);
        if (increment.intValue() > amountLimit) {
            log.info(" Bad Luck :{},{}", key, amountLimit);
        } else {
            log.info(" Good Luck :{},{}", key, amountLimit);
        }

        return ResponseUtils.res(currAmount.toString());
    }

}
